/*
 * TipTip
 * Copyright 2010 Drew Wilson
 * www.drewwilson.com
 * code.drewwilson.com/entry/tiptip-jquery-plugin
 *
 * Version 1.3   -   Updated: Mar. 23, 2010
 *
 * This Plug-In will create a custom tooltip to replace the default
 * browser tooltip. It is extremely lightweight and very smart in
 * that it detects the edges of the browser window and will make sure
 * the tooltip stays within the current window size. As a result the
 * tooltip will adjust itself to be displayed above, below, to the left 
 * or to the right depending on what is necessary to stay within the
 * browser window. It is completely customizable as well via CSS.
 *
 * This TipTip jQuery plug-in is dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

(function($) {
    $.fn.editable = function(options) {
	var defaults = {
	    'hrefAttribute': ''
	}, gopts = $.extend(defaults, options),
		toggleStatus = function(value, status) {
		    for (var val in value) {
			if (status !== val)
			    return val;
		    }
		},
		toggleClass = function(value, status) {
		    for (var val in value) {
			if (status !== val)
			    return value[val][1];
		    }
		},
		ajax = function(href, data, callback) {
                    var url = (href === '') ? gopts.hrefAttribute : href;
		    $.ajax({
			url: url,
			type: 'post',
			dataType: 'json',
			data: data,
			cache: false,
			success: callback,
			error: DWZ.ajaxError
		    });
		},
		handler = function(box, opts) {
		    switch (opts.html) {
			case 'amount':
			    if (opts.default === '') {
				return;
			    }
			case 'text':
			    box.html(opts.default).dblclick(function(e) {
                                e.stopPropagation();
				var length = opts.default.length;
				obj = $('<input type="text" size="'+ length+'" value="' + opts.default + '"/>');
                                box.html(obj);
				obj.bind('dblclick',function(e){e.stopPropagation();}).bind('blur', function() {
				    var val = obj.val();
				    if (val !== opts.default && val !== '') {
					var data = {db: opts.db, value: val, primary: opts.primary, id: opts.id};
					ajax(opts.hrefAttribute, data, function(json) {
					    DWZ.ajaxDone(json);
					    if (json[DWZ.keys.statusCode] == DWZ.statusCode.ok) {
						opts.default = val;
						box.html(opts.default);
					    }
					});
				    } else {
					box.html(opts.default);
				    }
				});
                                if (opts.html === 'amount' && $.fn.amount) 
				    obj.amount();
			    });
			    break;
			case 'switch':
			    if (opts.value === '' || opts.default === '') {
				return;
			    }
			    var title = opts.value[opts.default][0],
				    style = opts.value[opts.default][1],
				    obj = $('<a title="' + title + '" href="javascript:;" class="' + style + '"/>');
			    box.html(obj);
			    obj.bind('click', function() {
				var data = {db: opts.db, value: toggleStatus(opts.value, opts.default), primary: opts.primary, id: opts.id};
				ajax(opts.hrefAttribute, data, function(json) {
				    DWZ.ajaxDone(json);
				    if (json[DWZ.keys.statusCode] == DWZ.statusCode.ok) {
					opts.default = toggleStatus(opts.value, opts.default);
					var oldStyle = toggleClass(opts.value, opts.default);
					obj.removeClass(oldStyle).addClass(opts.value[opts.default][1]);
				    }
				});
				return false;
			    }).bind('dblclick', function(e){e.stopPropagation();});
			    break;
			default:
			    break;
		    }
		};

	return $.each(this, function() {
	    var box = $(this),
		    def = {
                        hrefAttribute: "",
			html: "text",
			db: "",
			value: "",
			default: "",
			primary: "id",
			id: "",
		    },
		    options = box.attr('data-params');
	    options = $.parseJSON(options);
	    var opts = $.extend(def, options);

	    if (opts.html === '' || (opts.hrefAttribute === '' && opts.db === '')) {
		return;
	    }
	    opts.id = box.parent('tr[target=sid_user]').attr('rel');
	    handler(box, opts);
	});
    };
})(jQuery);  	